package service

import (
	"singo/model"
	"singo/serializer"
	"singo/util"
)

// MistakeCreateService 管理新增需求的服务
type MistakeCreateService struct {
	Name   string `form:"name" json:"name" binding:"required"`
	Info   string `form:"info" json:"info"`
	Url    string `form:"url" json:"url"`
	Source string `form:"source" json:"source"`
}

// MistakeCreate 新增需求
func (service *MistakeCreateService) MistakeCreate(userUID string, userAgent string) *serializer.Response {
	strLocalIP := util.GetLocalIP()

	var createdBy string
	if userUID != "" {
		createdBy = userUID
	} else {
		createdBy = "未登录用户"
	}

	mistake := model.Mistake{
		Name:      service.Name,
		Info:      service.Info,
		Url:       service.Url,
		Ip:        strLocalIP,
		Source:    service.Source,
		UserAgent: userAgent,
		CreatedBy: createdBy,
	}

	// 创建错误日志
	if err := model.DB.Create(&mistake).Error; err != nil {
		return &serializer.Response{
			Status: 50001,
			Msg:    "错误日志保存失败",
			Error:  err.Error(),
		}
	}

	return &serializer.Response{
		Data:   true,
		Msg:    "成功",
		Status: 0,
	}
}
